<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="SN码" prop="snCode">
        <el-input v-model="queryParams.snCode" placeholder="请输入SN码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工单编码" prop="workorderCode">
        <el-input v-model="queryParams.workorderCode" placeholder="请输入工单编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="所属工位" prop="workPosition">
        <el-input v-model="queryParams.workPosition" placeholder="请输入工位名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['produce:selfTest:export']">导出</el-button>
      </el-col>
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
    ></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" border resizable :data="resultList" height="calc(100vh - 230px)"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="SN码" align="center" prop="snCode" min-width="110" />
      <el-table-column label="工单编码" align="center" prop="workorderCode" min-width="100" />
      <el-table-column label="所属工位" align="center" prop="workPosition" min-width="120" />
      <!-- <el-table-column label="工序编码" align="center" prop="processCode" /> -->
      <el-table-column label="产品名称" align="center" prop="productName" min-width="170" />
      <!-- <el-table-column label="检查项" align="center" prop="inspectionItem" /> -->
      <el-table-column label="工作流状态" align="center" prop="wfState" min-width="90">
        <template slot-scope="scope">
          <span>
            {{
              scope.row.wfState == "1"
                ? "工位自检"
                : scope.row.wfState == "2"
                  ? "检验员审核"
                  : scope.row.wfState == "3"
                    ? "自检合格"
                    : "--"
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="自检人" align="center" prop="createBy" />
      <el-table-column label="自检开始时间" align="center" prop="startTime" min-width="155">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="自检结束时间" align="center" prop="endTime" min-width="155">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批人" align="center" prop="examineManName">
        <template slot-scope="scope">
          <span>{{ scope.row.examineManName || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批时间" align="center" prop="examineDate" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.examineDate || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批结果" align="center" prop="examineResult">
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.examineResult  || "--"}}</span>
        </template> -->
        <template slot-scope="scope">
          <span>
            {{
              scope.row.examineResult == "0"
                ? "不合格"
                : scope.row.examineResult == "1"
                  ? "合格"
                  : "--"
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="审核原因" align="center" prop="examineReason">
        <template slot-scope="scope">
          <span>{{ scope.row.examineReason || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark">
        <template slot-scope="scope">
          <span>{{ scope.row.remark || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="manageQuery(scope.row)"
            v-if="scope.row.wfState == '2'">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handlePushDown(scope.row)"
            v-if="scope.row.wfState == '1'">下推</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="manageQuery(scope.row)"
            v-hasPermi="['produce:selfTest:auditView']">审核详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改工位自检结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单编码" prop="workorderCode">
          <el-input v-model="form.workorderCode" placeholder="请输入工单编码" />
        </el-form-item>
        <el-form-item label="工单名称" prop="workorderName">
          <el-input v-model="form.workorderName" placeholder="请输入工单名称" />
        </el-form-item>
        <el-form-item label="0不合格 1合格" prop="inspectionResult">
          <el-input v-model="form.inspectionResult" placeholder="请输入0不合格 1合格" />
        </el-form-item>
        <el-form-item label="工作站ID" prop="workstationId">
          <el-input v-model="form.workstationId" placeholder="请输入工作站ID" />
        </el-form-item>
        <el-form-item label="工作站编码" prop="workstationCode">
          <el-input v-model="form.workstationCode" placeholder="请输入工作站编码" />
        </el-form-item>
        <el-form-item label="工作站名称" prop="workstationName">
          <el-input v-model="form.workstationName" placeholder="请输入工作站名称" />
        </el-form-item>
        <!-- <el-form-item label="工序ID" prop="processId">
          <el-input v-model="form.processId" placeholder="请输入工序ID" />
        </el-form-item>-->
        <el-form-item label="工序编码" prop="processCode">
          <el-input v-model="form.processCode" placeholder="请输入工序编码" />
        </el-form-item>
        <el-form-item label="工序名称" prop="processName">
          <el-input v-model="form.processName" placeholder="请输入工序名称" />
        </el-form-item>
        <el-form-item label="检查项" prop="inspectionItem">
          <el-input v-model="form.inspectionItem" placeholder="请输入检查项" />
        </el-form-item>
        <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <!-- <el-form-item label="产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品id" />
        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-form-item label="产线id" prop="lineId">
          <el-input v-model="form.lineId" placeholder="请输入产线id" />
        </el-form-item>-->
        <el-form-item label="产线名称" prop="lineName">
          <el-input v-model="form.lineName" placeholder="请输入产线名称" />
        </el-form-item>
        <el-form-item label="车间编码" prop="workshopCode">
          <el-input v-model="form.workshopCode" placeholder="请输入车间编码" />
        </el-form-item>
        <el-form-item label="车间名称" prop="workshopName">
          <el-input v-model="form.workshopName" placeholder="请输入车间名称" />
        </el-form-item>
        <!-- <el-form-item label="车间id" prop="workshopId">
          <el-input v-model="form.workshopId" placeholder="请输入车间id" />
        </el-form-item>-->
        <el-form-item label="产线编号" prop="lineNo">
          <el-input v-model="form.lineNo" placeholder="请输入产线编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <CheckDetails ref="checkDetail" v-if="openCheck" @parentList="getList"></CheckDetails>
  </div>
</template>

<script>
import {
  listResult,
  getResult,
  delResult,
  addResult,
  updateResult,
  editPushDown
} from "@/api/mes/pro/result";
import { selectUserRole } from "@/api/mes/pro/workorder";
import CheckDetails from "./checkDetails.vue";
import moment from "moment";
export default {
  name: "Result",
  components: { CheckDetails },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工位自检结果表格数据
      resultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCheck: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderCode: null,
        workorderName: null,
        inspectionResult: null,
        workstationId: null,
        workstationCode: null,
        workPosition: null,
        processId: null,
        processCode: null,
        processName: null,
        inspectionItem: null,
        productCode: null,
        productId: null,
        lineId: null,
        lineName: null,
        workshopCode: null,
        workshopName: null,
        workshopId: null,
        lineNo: null,
        productName: null,
        snCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工位自检结果列表 */
    getList() {
      this.loading = true;
      listResult(this.queryParams).then(response => {
        this.resultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workorderCode: null,
        workorderName: null,
        inspectionResult: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        processId: null,
        processCode: null,
        processName: null,
        inspectionItem: null,
        productCode: null,
        productId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        lineId: null,
        lineName: null,
        workshopCode: null,
        workshopName: null,
        workshopId: null,
        lineNo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工位自检结果";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getResult(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工位自检结果";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */

    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除工位自检结果编号为"' + ids + '"的数据项？')
        .then(function () {
          return delResult(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/station/stationCheck/export",
        {
          ...this.queryParams
        },
        `工位自检记录_${moment(new Date()).format('yyyyMMDDHHmmss')}.xlsx`
      );
    },

    handleCheck(row) {
      // this.parentId = row.receiptId;
      // console.log(this.parentId);
      // this.$refs.receiptDetails.showFlag = true;
      this.openCheck = true;
      this.$nextTick(() => {
        this.$refs.checkDetail.init(row);
      });
    },
    // manage
    manageQuery(row) {
      this.handleCheck(row);
      // let name = row.workflowStatus == "0" ? "SQE审核" : row.workflowStatus == "1" ? "品质部审核" : null;
      // if (name) {
      //   selectUserRole(name)
      //     .then(res => {
      //       this.handleCheck(row);
      //     })
      //     .catch(() => {
      //       return "false";
      //     });
      // } else {
      //   this.$modal.msgError("该通知单不需要审核！！！");
      // }
    },
    // 下推
    handlePushDown(row) {
      let query = { ...row };
      // query.flowType =
      this.$modal
        .confirm(
          '是否确认下推生产工单编号为"' + row.workorderCode + '"的数据项？'
        )
        .then(function () {
          return editPushDown(query);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("下推成功");
        })
        .catch(() => { });
    }
  }
};
</script>
